package com.ldcc.report_system.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.ldcc.report_system.R;
import com.ldcc.report_system.api.results.GuidePageElement;
import com.ldcc.report_system.databinding.GuidePageItemBinding;
import com.ldcc.report_system.ui.MainActivity;
import com.ldcc.report_system.util.ActivitySkipUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wwg
 * @version JDK 11
 * @description 引导页适配器
 * @create 2021-10-22 16:31
 **/
public class GuidePageAdapter extends RecyclerView.Adapter<GuidePageHolder> {

    // 界面元素集合
    private final List<GuidePageElement> guidePageElementList = new ArrayList<>();

    private Context mContext;

    private GuidePageItemBinding guidePageItemBinding;

    // 当前的 Activity
    private Activity activity;

    public GuidePageAdapter(Activity activity) {
        this.activity = activity;
        // 设置页面数据
        GuidePageElement guidePageElement1 = new GuidePageElement() {{
            setImage(R.drawable.office_page);
            setText1(R.string.any_time);
            setText2(R.string.writing);
            setText3(R.string.any_time_any_place);
            setText4(R.string.no_waiting);
            setText5(R.string.efficient_work);
            setGuideDot(3, 0);
        }};
        GuidePageElement guidePageElement2 = new GuidePageElement() {{
            setImage(R.drawable.office_meeting);
            setText1(R.string.every_day);
            setText2(R.string.look);
            setText3(R.string.smart_work);
            setText4(R.string.convenient_to_record);
            setText5(R.string.actively_communicate);
            setGuideDot(3, 1);
        }};
        GuidePageElement guidePageElement3 = new GuidePageElement() {{
            setImage(R.drawable.office_man);
            setText1(R.string.every_week);
            setText2(R.string.clean);
            setText3(R.string.technology_help);
            setText4(R.string.face_the_problem);
            setText5(R.string.solve_the_problem);
            setGuideDot(3, 2);
        }};
        guidePageElementList.add(guidePageElement1);
        guidePageElementList.add(guidePageElement2);
        guidePageElementList.add(guidePageElement3);
    }

    @NonNull
    @Override
    public GuidePageHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        this.mContext = parent.getContext();
        this.guidePageItemBinding = GuidePageItemBinding.inflate(LayoutInflater.from(mContext));
        // 设置 viewpage2 长宽为 match_parent
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        this.guidePageItemBinding.getRoot().setLayoutParams(lp);
        return new GuidePageHolder(this.guidePageItemBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull GuidePageHolder holder, int position) {
        // 绑定数据
        holder.getGuidePageItemBinding().imageView.setImageResource(guidePageElementList.get(position).getImage());
        holder.getGuidePageItemBinding().textView1.setText(guidePageElementList.get(position).getText1());
        holder.getGuidePageItemBinding().textView2.setText(guidePageElementList.get(position).getText2());
        holder.getGuidePageItemBinding().textView3.setText(guidePageElementList.get(position).getText3());
        holder.getGuidePageItemBinding().textView4.setText(guidePageElementList.get(position).getText4());
        holder.getGuidePageItemBinding().textView5.setText(guidePageElementList.get(position).getText5());
        holder.getGuidePageItemBinding().guideDot1.setBackgroundResource(guidePageElementList.get(position).getDotList().get(0));
        holder.getGuidePageItemBinding().guideDot2.setBackgroundResource(guidePageElementList.get(position).getDotList().get(1));
        holder.getGuidePageItemBinding().guideDot3.setBackgroundResource(guidePageElementList.get(position).getDotList().get(2));
        // 设置点击跳过的事件
        guidePageItemBinding.skip.setOnClickListener(view -> {
            ActivitySkipUtil.skipAnotherActivity(getActivity(), MainActivity.class);
        });
    }

    @Override
    public int getItemCount() {
        return guidePageElementList.size();
    }

    public Activity getActivity() {
        return activity;
    }

    public void setActivity(Activity activity) {
        this.activity = activity;
    }
}
